package us.esme.managers
{
       import flash.filesystem.FileMode;
       import flash.filesystem.FileStream;
       import flash.filesystem.File;

       import mx.collections.ArrayCollection;

		// TODO: Have the ability to store some prefs like auth token in encrypted datastore
		// also perhaps this not the best place to read files .. mixes model and controller 

       public class SettingsManager
       {
               //For config file
               private var file:File;
               private var stream:FileStream;

               //Preferences
               [Bindable] public var preferences:ArrayCollection = new ArrayCollection();
               private var prefXML:XML;

               public function SettingsManager()
               {
                       file = File.applicationStorageDirectory.resolvePath("preferences.xml");
                       stream = new FileStream();
                       readPreferences();
               }



               public function writePreferences():void
               {
                       var output:String = new String();

                       prefXML = new XML("<preferences/>");

                       for each (var preference:Object in preferences)
                       {
                               var sxml:String = new String("<preference name =\"" + preference.name + "\" value=\"" + preference.value + "\"/>");

                               prefXML.appendChild(XML(sxml));
                       }

                       stream.open(file, FileMode.WRITE);

                       output = prefXML.toXMLString();
                       output = output.replace(/\n/g, File.lineEnding);

                       stream.writeUTFBytes(output);

                       stream.close();
               }

               private function readPreferences():void
               {
                       if(file.exists){

	                       stream.open(file, FileMode.READ);
	                       if (stream.bytesAvailable == 0)
	                       {
	                               prefXML = new XML("<preferences/>");
	                       }else
	                       {
	                               prefXML = XML(stream.readUTFBytes(stream.bytesAvailable));
	
	                               if (prefXML != null)
	                               {
	                                       for each (var pref:XML in prefXML.preference)
	                                       {
	                                               var obj:Object = new Object();
	
	                                               obj.name = pref.@name;
	                                               obj.value = pref.@value;
	
	                                               preferences.addItem(obj);
	                                       }
	                               }
	                       }
                       	   stream.close();
                       }else{
                       		prefXML = new XML("<preferences/>");
                       }
                       
               }

               public function getPreferences():ArrayCollection
               {
                       return preferences;
               }

               public function getPreference(name:String):String
               {
                       for each (var preference:Object in preferences)
                       {
                               if (preference.name == name)
                               {
                                       return preference.value;
                               }
                       }
                       return "";
               }


               public function setPreference(name:String, value:String):void
               {
                       var flag:Boolean = new Boolean(false);

                       for each (var preference:Object in preferences)
                       {
                               if (preference.name == name)
                               {
                                       preference.value = value;
                                       flag = true;
                               }
                       }

                       if (flag == false)
                       {
                               var obj:Object = new Object();

                               obj.name = name;
                               obj.value = value;

                               preferences.addItem(obj);
                       }
                       
                       
                       writePreferences();
               }
       }
}